/**
 * Title: BaseEntity.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2013-8-9 下午5:11:27
 * Version 1.0 
 */
package com.luoxudong.phonenumber.service.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/** 
 * ClassName: BaseEntity 
 * Description:TODO(这里用一句话描述这个类的作用) 
 * Create by 罗旭东
 * Date 2013-8-9 下午5:11:27 
 */
public class BaseEntity<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 返回深拷贝副本方法
	 * 
	 * @return 深拷贝的副本
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public T copyEntity()
	{
		ByteArrayOutputStream byteArrayOutputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try
		{
			byteArrayOutputStream = new ByteArrayOutputStream();
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(this);

			ByteArrayInputStream byteIn = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

			ObjectInputStream in = new ObjectInputStream(byteIn);
			T result = (T) in.readObject();
			return result;
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (byteArrayOutputStream != null)
			{
				try
				{
					byteArrayOutputStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
			if (objectOutputStream != null)
			{
				try
				{
					objectOutputStream.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException
	{
		return copyEntity();
	}
}
